package everyday;

import java.util.Arrays;

/**
 * @Author: Siantar
 * @Date: 2023-06-01-20:17
 * @Description: 1.0
 */
public class T2517 {
    public int maximumTastiness(int[] price, int k) {
        Arrays.sort(price);
        int left = 0;
        int right = price[price.length - 1] - price[0];
        while (left < right) {
            int mid = (left + right + 1) / 2;
            if (two(price, k, mid)) {
                left = mid;
            } else {
                right = mid - 1;
            }
        }
        return left;
    }

    public boolean two(int[] price, int k, int t) {
        int x = Integer.MIN_VALUE / 2;
        int cnt = 0;
        for (int p : price) {
            if (p - t >= x) {
                x = p;
                cnt++;
            }
        }
        return cnt >= k;
    }
}
